package com.qf.controller;

import com.qf.entity.Emp;
import com.qf.service.EmpService;
import com.qf.service.impl.EmpServiceImpl;
import com.qf.uitl.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class Demo1Controller {
    //注入 service
    @Autowired
    private EmpService EmpService = new EmpServiceImpl();
    @PostMapping("/Emp/add")
    public R addEmp(Emp emp){
        System.out.println("接收的参数：" + emp); // 查看各属性是否为null
        //调用 service
        int rows = EmpService.addEmp(emp);
        if(rows > 0) {
            return R.success("添加成功");
        } else {
            return R.fail();
        }
    }

    @GetMapping("/Emp/delete")
    public R deleteEmp(String ename){
        int rows = EmpService.deleteEmp(ename);
        if(rows > 0) {
            return R.success("删除成功");
        } else {
            return R.fail();
        }
    }

    @GetMapping("/Emp/update")
    public R updateEmp(Emp emp){
        int rows = EmpService.updateEmp(emp);
        if(rows > 0) {
            return R.success("修改成功");
        } else {
            return R.fail();
        }
    }

    @GetMapping("/Emp/query")
    public R queryEmp(String ename){
        Emp emp = EmpService.queryEmp(ename);
        if(emp != null) {
            return R.success(emp);
        } else {
            return R.fail();
        }
    }

    @GetMapping("/Emp/listByMap")
    public R listByMap(Map<String, String> map){
        System.out.println("Controller接收前端发送的数据: " + map);
        List<Emp> list = EmpService.listByMap(map);
        return R.success(list);
    }


}
